//
//  RWHomePageRootViewController.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/13.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWHomePageRootViewController.h"
#import "RWMessageViewController.h"
#import "KFSearchViewController.h"
#import "FTYMenuViewController.h"
#import "RWHomePageViewController.h"
#import "FTYNavigationController.h"
#import "RWLoginViewController.h"

@interface RWHomePageRootViewController ()

@property (nonatomic, strong) FTYMenuViewController *menuVC;
@property (nonatomic, assign) NSInteger chooseIndex;

@end

@implementation RWHomePageRootViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigation];
    [self setMenuUI];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if ([FTYUser sharedUser].userID.length > 0) {
        
        [self loadDatas];
    } else {
        
        RWLoginViewController *loginVC = [[RWLoginViewController alloc] init];
        FTYNavigationController *loginNC = [[FTYNavigationController alloc] initWithRootViewController:loginVC];
        loginNC.navigationBar.translucent = NO;
        loginNC.navigationBar.barTintColor = [FTYColor getMainColorWithAlpha:1.];
        loginNC.navigationBar.tintColor = [UIColor whiteColor];
        [loginNC.navigationBar setTitleTextAttributes:@{NSFontAttributeName: FTY_FONTTSIZE(20),
                                                        NSForegroundColorAttributeName: [UIColor whiteColor]}];
        [self presentViewController:loginNC animated:YES completion:nil];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{
    
    
}

- (void)loadDatas{
    
    
}

#pragma mark - Config UI

- (void)setNavigation{
    
    UIButton *bottomBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 300.*FTY_WIDTH_BASE, 35.*FTY_HEIGHT_BASE)];
    bottomBtn.backgroundColor = [UIColor whiteColor];
    bottomBtn.layer.masksToBounds = YES;
    bottomBtn.layer.cornerRadius = 3.*FTY_WIDTH_BASE;
    [bottomBtn addTarget:self action:@selector(bottomBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //搜索
    UIButton *searchIcon = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 33.*FTY_WIDTH_BASE, 33.*FTY_HEIGHT_BASE)];
    [bottomBtn addSubview:searchIcon];
    [searchIcon setImage:[UIImage imageNamed:@"search_icon"] forState:UIControlStateNormal];
    
    //文字
    UILabel *searchLab = [[UILabel alloc] init];
    [bottomBtn addSubview:searchLab];
    searchLab.text = @"搜索全部商品";
    searchLab.textColor = [FTYColor grayColor];
    searchLab.font = FTY_FONTTSIZE(15.);
    [searchLab sizeToFit];
    [searchLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(searchIcon.mas_right).offset(10.*FTY_WIDTH_BASE);
    }];
    
    self.navigationItem.titleView = bottomBtn;
    
    UIButton *messageBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 33.*FTY_WIDTH_BASE, 33.*FTY_HEIGHT_BASE)];
    [messageBtn setImage:[UIImage imageNamed:@"infor_bj_icon"] forState:UIControlStateNormal];
    [messageBtn addTarget:self action:@selector(messageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *messageBarItem = [[UIBarButtonItem alloc] initWithCustomView:messageBtn];
    self.navigationItem.rightBarButtonItem = messageBarItem;
}

- (void)setMenuUI{
    
    NSMutableArray *ctls = [NSMutableArray array];
    NSMutableArray *hometopTitles = [NSMutableArray arrayWithObjects:@"全部", @"礼盒包装", @"法国", @"澳洲", @"智利", @"西班牙", @"意大利", @"美国", nil];
    NSMutableArray *fromIDs = [NSMutableArray arrayWithObjects:@"", @"3235", @"3228", @"3233", @"3232", @"3229", @"3230", @"3234 ", nil];
    for (int i=0; i<hometopTitles.count; i++) {
        
        RWHomePageViewController *homePageVC = [[RWHomePageViewController alloc] init];
        homePageVC.fromID = fromIDs[i];
        if (i>0) {
            homePageVC.fromFlag = @"产地";
        }
        [ctls addObject:homePageVC];
    }
    
    if (!_menuVC) {
        _menuVC = [FTYMenuViewController segmentControllerWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT) titles:hometopTitles];
        [_menuVC setSegmentTintColor:[FTYColor whiteColor]];
        _menuVC.style = FTYSegmentStyleDefault;
        WeakSelf(ws);
        [_menuVC selectedAtIndex:^(NSInteger index) {
            NSLog(@"index %ld", (long)index);
            ws.chooseIndex = index;
        }];
    }
    [_menuVC updateSegmentView:hometopTitles];
    [_menuVC setSegmentViewControllers:ctls];
    [_menuVC setDefaultSelectItemAtIndex:_chooseIndex];
    
    [self.view addSubview:_menuVC.view];
    [self addChildViewController:_menuVC];
    
    //DLog(@"_menuVC:frame:%@",NSStringFromCGRect(_menuVC.view.frame));
}

#pragma mark - Config Invoke Methods

- (void)bottomBtnAction:(UIButton *)btn{
    
    //搜索
    KFSearchViewController *searchVC = [[KFSearchViewController alloc] init];
    searchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)messageBtnAction:(UIButton *)btn{
    
    //消息
    RWMessageViewController *messageVC = [[RWMessageViewController alloc] init];
    messageVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:messageVC animated:YES];
}

#pragma mark - Notification Method

#pragma mark - Notification Invoke Methods

#pragma mark - <UITableViewDataSource>

#pragma mark - <UITableViewDelegate>

#pragma mark - Private Invoke Methods

@end
